/*
 * sensors.h
 *
 *  Created on: 25 apr. 2013
 *      Author: Pascal
 */

#ifndef SENSORS_H_
#define SENSORS_H_

#include "sensorvalue_generation.h"
#include <string.h>
#include <time.h>
#include <stdio.h>

/**
 * Amount of sensors
 */
// Test amount
//#define ANALOG_SENSORS_10MS_AMOUNT 3
//#define ANALOG_SENSORS_100MS_AMOUNT 3
//#define BINARY_SENSORS_AMOUNT 3
// Real amount
#define ANALOG_SENSORS_10MS_AMOUNT 16
#define ANALOG_SENSORS_100MS_AMOUNT 64
#define BINARY_SENSORS_AMOUNT 384

#define ANALOG_SENSORS_TOTAL_AMOUNT ((ANALOG_SENSORS_10MS_AMOUNT)+(ANALOG_SENSORS_100MS_AMOUNT))
#define SENSORS_TOTAL_AMOUNT ((ANALOG_SENSORS_10MS_AMOUNT)+(ANALOG_SENSORS_100MS_AMOUNT)+(BINARY_SENSORS_AMOUNT))

/**
 * Default values for sensors
 */
#define ANALOG_SENSOR_DEFAULT_NAME "AnalogSensor"
#define ANALOG_SENSOR_DEFAULT_LOWER_LIMIT 47.0
#define ANALOG_SENSOR_DEFAULT_LOWER_ALARM_NAME "Lower limit reached!"
#define ANALOG_SENSOR_DEFAULT_UPPER_LIMIT 53.0
#define ANALOG_SENSOR_DEFAULT_UPPER_ALARM_NAME "Upper limit reached!"
#define ANALOG_SENSOR_DEFAULT_UNIT "Pascal"
#define ANALOG_SENSOR_DEFAULT_VALUE 50.0
#define BINARY_SENSOR_DEFAULT_NAME "BinarySensor"
#define BINARY_SENSOR_DEFAULT_ALARM_NAME "Binary sensor limit reached!"
#define SENSOR_DEFAULT_TIMESTAMP "0000/00/00/00/00/00/0000"

/**
 * Defines used for initializing analog sensors with random values
 */
#define SENSOR_MAX_UPPER_LIMIT 70.0
#define SENSOR_MIN_UPPER_LIMIT 20.0
#define SENSOR_MAX_LOWER_LIMIT_DEVIATION 30.0
#define SENSOR_MIN_LOWER_LIMIT_DEVIATION 10.0

#define BINARY_SENSOR_NO_ALARM "no alarm"

typedef struct AnalogSensor {
	char name[41];
	char unit[16];
	double value;
	char timeStamp[40]; //(year/month/day/hour/minute/second/ms)
	char lowerLimitAlarmName[65];
	char upperLimitAlarmName[65];
	double lowerLimit;
	double upperLimit;
};

typedef struct BinarySensor {
	char name[41];
	//char unit[16]; // Don't think this is necessary... ???
	int value; // 0 for false, 1 for true
	char alarmName[65]; // Name of the alarm, "no alarm" disabled
	char timeStamp[40]; //(year/month/day/hour/minute/second/ms)
};

typedef struct AnalogAlarmChangeStruct {
	char name[41];
	char lowerLimitAlarmName[65];
	char upperLimitAlarmName[65];
	double lowerLimit;
	double upperLimit;
};

typedef struct BinaryAlarmChangeStruct {
	char name[41];
	int value; // 0 for false, 1 for true
	char timeStamp[21]; //(year/month/day/hour/minute/second/ms)
	char alarmName[65]; // Name of the alarm, "no alarm" disabled
};

void initSensors();
void read10msSensorValues();
void read100msSensorValues();
void check10msSensorsForAlarms();
void check100msSensorsForAlarms();
int analogSensorExceedsLimits(struct AnalogSensor*);
int binarySensorExceedsLimits(struct BinarySensor*);
void printSensors();
void getTimeStamp(char stamp[]);
void findAndChangeAnalogAlarm(struct AnalogAlarmChangeStruct*);
void findAndChangeBinaryAlarm(struct BinaryAlarmChangeStruct*);

/**
 * Arrays for the sensors.
 */
struct AnalogSensor analogSensors10ms[ANALOG_SENSORS_10MS_AMOUNT];
struct AnalogSensor analogSensors100ms[ANALOG_SENSORS_100MS_AMOUNT];
struct BinarySensor binarySensors[BINARY_SENSORS_AMOUNT];

#endif /* SENSORS_H_ */
